//
//  HomeViewController.m
//  BigTyre
//
//  Created by 田涛 on 2017/3/15.
//  Copyright © 2017年 Lemon. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeDataShared.h"
#import "HomeModel.h"
#import "MainTableViewCell.h"
#import "MainTableViewCell_FirstSection.h"
#import "MainTableViewCell_OptionView.h"
#import "MainTableHeaderView.h"
#import "HomeOptionViewController.h"
#import "Common.h"

#import <SDCycleScrollView/SDCycleScrollView.h>
#import <SDAutoLayout.h>
#import <MJRefresh.h>
#import <AFNetworking.h>

@interface HomeViewController () <SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource> {
    SDCycleScrollView *_cycleScrollView;
    MainOptionView *_optionView;
}
@property (nonatomic, strong) NSMutableArray *scrollArr;
@property (nonatomic, strong) NSMutableArray *optionArr;
@property (nonatomic, strong) NSMutableArray *productArr;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.tableView];
    [self getNetData];
    [self initSDCycleScrollView];
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}

- (void)initSDCycleScrollView {
    _cycleScrollView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 220)];
    [_cycleScrollView setImageURLStringsGroup:_scrollArr];
    _tableView.tableHeaderView = _cycleScrollView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 取消分割线
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        // 将轮播图设为表格的头视图
        _tableView.backgroundView = [[UIView alloc]init];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNetData)];
    }
    return _tableView;
}

#pragma mark 获取数据
- (void)getNetData {
    
    [[HomeDataShared sharedData] getURL:HOME_URL];
    
    HomeDataShared *dataShared = [[HomeDataShared alloc] init];
    dataShared.productBlock = ^(id data) {
        _productArr = data;
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    };
    
    dataShared.scrollBlock = ^(id data) {
        _scrollArr = data;
        [self initSDCycleScrollView];
    };
    
    dataShared.optionBlock = ^(id data) {
        _optionArr = data;
        [self.tableView reloadData];
    };
}

#pragma mrak - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1){
        return 1;
    } else if (section == 2) {
        return _optionArr.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *main_first = @"Main_FirstSectionCell";
    static NSString *main_option = @"Main_OptionViewCell";
    static NSString *main_product = @"MainCell";
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:main_first];
    }
    
    switch (indexPath.section) {
        case 0: {
            MainTableViewCell_FirstSection *cellFirst = [tableView cellForRowAtIndexPath:indexPath];
            if (!cellFirst) {
                cellFirst = [[MainTableViewCell_FirstSection alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:main_first ];
            }
            [cellFirst setSelectionStyle:UITableViewCellSelectionStyleNone];
            return cellFirst;
        }
            break;
        case 1: {
            MainTableViewCell_OptionView *cellOption = [tableView cellForRowAtIndexPath:indexPath];
            if (!cellOption) {
                cellOption = [[MainTableViewCell_OptionView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:main_option data:_optionArr];
            }
            [cellOption setSelectionStyle:UITableViewCellSelectionStyleNone];
            cellOption.pushBlock = ^(NSString *sid,NSString *title) {
                HomeOptionViewController *hoVC = [[HomeOptionViewController alloc] init];
                hoVC.url = [NSString stringWithFormat:HOME_OPTION_URL,sid];
                hoVC.title = title;
                [self.navigationController pushViewController:hoVC animated:YES];
            };
            return cellOption;
        }
            break;
        case 2: {
            MainTableViewCell *cellMain = [tableView cellForRowAtIndexPath:indexPath];
            if (!cellMain) {
                cellMain = [[MainTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:main_product ];
            }
            [cellMain setSelectionStyle:UITableViewCellSelectionStyleNone];
            cellMain.model = _productArr[indexPath.row];
            return cellMain;
        }
            break;
            
        default:
            break;
    }
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return WIDTH / 4;
    } else if (indexPath.section == 1) {
        return OPTION_HEIGHT;
    } else {
        HomeModel_Product *model = _productArr[indexPath.row];
        return [tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[MainTableViewCell class] contentViewWidth:[UIScreen mainScreen].bounds.size.width];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 50;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        MainTableHeaderView *headerV = [[MainTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40) image:@"home_option" title:@"分类"];
        return headerV;
    } else if (section == 2) {
        MainTableHeaderView *headerV = [[MainTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40) image:@"home_product" title:@"精品推荐"];
        return headerV;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

@end
